﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Threading.Tasks;
using System.IO.IsolatedStorage;
using Tin18.Library;
using Tin18.Resources;
using System.Xml.Linq;
using Newtonsoft.Json;
using Tin18.Model;

namespace Tin18
{
    public partial class SplashScreen : PhoneApplicationPage
    {
        public SplashScreen()
        {
            InitializeComponent();
            App.LoadLike();
            CheckVersion();
            RegisterUser();
        }

        public void CheckVersion()
        {
        }

        async void Splash_Screen()
        {
            await Task.Delay(TimeSpan.FromSeconds(1)); // set your desired delay
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); // call MainPage

        }

        public void RegisterUser()
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
   
            if (!settings.Contains("UserID"))
            {
                byte[] id = (byte[])Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("DeviceUniqueId");
                string deviceID = MyLibrary.GetMD5(Convert.ToBase64String(id));
                string Version = XDocument.Load("WMAppManifest.xml").Root.Element("App").Attribute("Version").Value.Substring(0, 5);
                string temp = System.Environment.OSVersion.Version.Major + "." + System.Environment.OSVersion.Version.Minor;
                string Osversion = temp.Substring(0, 3);
              //  string Osversion = "17";
                string Param = MyLibrary.RanDomBase64() + MyLibrary.Base64(string.Format(AppResources.RegisterUser, deviceID, Version, Osversion));
                string URL = AppResources.Server + Param;
                WebClient webclient = new WebClient();
                webclient.DownloadStringCompleted += (s, e) =>
                    {
                        try
                        {
                            string json = e.Result;
                            if (!string.IsNullOrEmpty(json))
                            {
                                RootUserObject _Object = JsonConvert.DeserializeObject<RootUserObject>(json);
                                App.UserID = _Object.Id;
                                settings.Add("UserID", _Object.Id);
                                settings.Save();
                                Splash_Screen();
                            }
                  
                        }
                        catch
                        {
                            Splash_Screen();
                        }
                    };
                webclient.DownloadStringAsync(new Uri(URL));
            }
            else
            {
                App.UserID = settings["UserID"].ToString();
                Splash_Screen();
            }
     
        }
    }
}